package com.zjw.touchscreen.listener;

import android.annotation.SuppressLint;
import android.bluetooth.*;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.zjw.touchscreen.bluetooth.BluetoothListItem;
import com.zjw.touchscreen.bluetooth.BluetoothUtils;
import lombok.SneakyThrows;

public class VoiceConnectClickListener implements View.OnClickListener {

    private Handler mainHandler = new Handler();

    private Spinner spinner;
    private Context context;
    private int state;

    public VoiceConnectClickListener(Context context, Spinner spinner) {
        this.context = context;
        this.spinner = spinner;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @SuppressLint("MissingPermission")
    @Override
    public void onClick(View v) {
        v.setEnabled(false);
        closeConnect();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                long now = System.currentTimeMillis();
                while (state != BluetoothHidDevice.STATE_DISCONNECTED) {
                    if(System.currentTimeMillis() - now>1000*30){
                        Toast.makeText(context,"蓝牙连接超时！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Thread.sleep(500);
                }
                Thread.sleep(1000);
                mainHandler.post(() -> v.setEnabled(true));
                BluetoothListItem bluetoothListItem = (BluetoothListItem) spinner.getSelectedItem();
                BluetoothUtils.setBluetoothDevice(bluetoothListItem.getBluetoothDevice());

                BluetoothUtils.getBluetoothDevice().createBond();
                //获取bluetoothHidDevice
                BluetoothUtils.getBluetoothAdapter().getProfileProxy(context, new BluetoothProfile.ServiceListener() {
                    @RequiresApi(api = Build.VERSION_CODES.P)
                    @Override
                    public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
                        if (profile != BluetoothProfile.HEADSET){
                            return;

                        }
                        BluetoothHeadset headset = (BluetoothHeadset) bluetoothProfile;
                        // 使用蓝牙录音
                        boolean audioConnected = headset.startVoiceRecognition(BluetoothUtils.getBluetoothDevice());
                        BluetoothUtils.setBluetoothHeadset(headset);

                    }

                    @Override
                    public void onServiceDisconnected(int profile) {
                        // 停止录音
                        BluetoothUtils.getBluetoothHeadset().stopVoiceRecognition(BluetoothUtils.getBluetoothDevice());
                        // 关闭配置文件代理与服务的连接
                        BluetoothUtils.getBluetoothAdapter().closeProfileProxy(profile, BluetoothUtils.getBluetoothHeadset());
                    }
                }, BluetoothProfile.HEADSET);
            }
        }).start();

    }

    @SuppressLint("MissingPermission")
    @RequiresApi(api = Build.VERSION_CODES.P)
    private void closeConnect() {
        BluetoothAdapter bluetoothAdapter = BluetoothUtils.getBluetoothAdapter();
        BluetoothHidDevice bluetoothHidDevice = BluetoothUtils.getBluetoothHidDevice();
        if (bluetoothHidDevice != null && bluetoothAdapter != null) {
            bluetoothHidDevice.unregisterApp();
            bluetoothAdapter.closeProfileProxy(BluetoothProfile.HID_DEVICE, bluetoothHidDevice);
        }
    }
}
